from py2neo.ogm import GraphObject, Property, RelatedTo, RelatedFrom, Label
from py2neo import Node, Relationship,Graph

from common.sql_define import project_info_sql,project_contact_sql
from db_utils.mysql_utils import MysqlInstance

from db_utils.dict_utils import split_values_from_keys,remove_None_value,make_neo4j_name
from common.sql_dict import convert_dict_entiprise,convert_dict_contactor,convert_dict_project,neo4j_name_list,convert_zhongbiao_relation
from pprint import  pprint
from db_utils.neo4j_utils import create_neo4j_node_merge_string,create_neo4j_relation_merge_string
from tqdm import tqdm

graph = Graph('http://0.0.0.0:7474', username='neo4j', password='123456')
mysql_obj = MysqlInstance()
results = mysql_obj.execute_sql(project_info_sql)

from pprint import pprint
for result in tqdm(results[13000:]):
    insert_list = []
    remove_None_value(result)

    project_info = split_values_from_keys(result, convert_dict_project)
    enprise_info = split_values_from_keys(result, convert_dict_entiprise)
    contactor_info = split_values_from_keys(result, convert_dict_contactor)
    zhongbiao_relation_info = split_values_from_keys(result, convert_zhongbiao_relation)

    make_neo4j_name(project_info, neo4j_name_list)
    make_neo4j_name(enprise_info, neo4j_name_list)
    make_neo4j_name(contactor_info, neo4j_name_list)


    project_insert_str = create_neo4j_node_merge_string("工程项目","m1",project_info)
    enprise_insert_str = create_neo4j_node_merge_string('企业','m2',enprise_info)
    contactor_insert_str = create_neo4j_node_merge_string('项目负责人','m3',contactor_info)
    insert_list.append(project_insert_str)
    insert_list.append(enprise_insert_str)
    insert_list.append(contactor_insert_str)


    zhongbiao_relation_insert_str = create_neo4j_relation_merge_string("m1","m2",'中标单位',zhongbiao_relation_info)
    toubiao_relation_insert_str = create_neo4j_relation_merge_string("m2","m1",'投标')
    zhongbiaodanweifuzeren_relation_insert_str =  create_neo4j_relation_merge_string("m1","m3",'中标单位负责人')
    contactor_enterprise_relation_insert_str = create_neo4j_relation_merge_string('m3',"m2","就职")
    insert_list.append(zhongbiao_relation_insert_str)
    insert_list.append(toubiao_relation_insert_str)
    insert_list.append(zhongbiaodanweifuzeren_relation_insert_str)
    insert_list.append(contactor_enterprise_relation_insert_str)




    search_sql = project_contact_sql % result["crm_tender_announcement_id"]
    project_owner_info = mysql_obj.execute_sql(search_sql)


    current_var_num = 5
    for index, info in enumerate(project_owner_info):
        if info['enterprise_name']:
            tempdict = {}
            tempdict["enterprise_name"] = info["enterprise_name"]
            tempdict["name"] = info["enterprise_name"]

            enprise_insert_str_temp = create_neo4j_node_merge_string('enprise', 'm'+str(index+current_var_num), enprise_info)
            insert_list.append(enprise_insert_str_temp)

            if info['role']:
                info['role'] = info['role'].replace("'","")
                relation_tmp = create_neo4j_relation_merge_string('m1', 'm'+str(index+current_var_num), info['role'])
                insert_list.append(relation_tmp)

    insert_str = "\n".join(insert_list)

    print(insert_str)
    try:
        graph.run(insert_str)
    except Exception as e:
        print(e)
    break





